package com.zte.login.service.impl;

import com.zte.login.mapper.UserMapper;
import com.zte.login.pojo.UserJournal;
import com.zte.login.pojo.UserModule;
import com.zte.login.pojo.Users;
import com.zte.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

/**
 * @author luode
 * @Auther: 许月
 * @Date: 2022/10/01/17:23
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 登录专用+日志记录
     * @return 用户
     */
    @Override
    public Users login(Users users) {
        Users loginUser = userMapper.queryUser(users);
        if (loginUser != null) {
            UserJournal userJournal = new UserJournal();
            userJournal.setUId(loginUser.getUid());
            userJournal.setJcontent("用户登录");
            userJournal.setModuleId(5);
            userJournal.setJdate(new Date());
            userMapper.addLoginLog(userJournal);
        }
        return loginUser;
    }

    /**
     * 通过 id 查询Users
     * @param id 参数
     * @return Users
     */
    @Override
    public Users getUserById(Integer id) {
        Users user = null;
        if(id != null && !"".equals(id)){
            user = userMapper.getUserById(id);
        }
        return user;
    }

    /**
     * 查询角色所拥有的权限
     * @param users 用户
     * @return 用户权限对象集合
     */
    @Override
    public TreeMap<String, ArrayList<UserModule>> getModule(Users users) {
        ArrayList<UserModule> module = userMapper.getModule(users);
        TreeMap<String, ArrayList<UserModule>> listTreeMap = new TreeMap<>();
        for (UserModule userModule : module) {
            ArrayList<UserModule> moduleList = getModuleList(userModule.getModule_rname());
            listTreeMap.put(userModule.getModule_rname(), moduleList);
        }
        return listTreeMap;
    }

    /**
     * 通过 jobId 查询数据
     * @param jobId 职位id
     * @return 用户对象集合
     */
    @Override
    public List<Users> queryUsersListByAjax(Integer jobId) {
        List<Users> usersList = null;
        if(jobId != null &&  !"".equals(jobId)){
            usersList = userMapper.getUsersListByJobId(jobId);
        }
        return usersList;
    }

    /**
     * 查询权限二级菜单
     * @param module 权限名字
     * @return 用户权限对象集合
     */
    private ArrayList<UserModule> getModuleList(String module) {
        return userMapper.getModuleList(module);
    }


}
